package com.qianniu.napi.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Strings;
import com.qianniu.napi.admin.entity.*;
import com.qianniu.napi.admin.entity.penum.StatusEnum;
import com.qianniu.napi.admin.service.ICommentService;
import com.qianniu.napi.admin.service.IGoodsfavService;
import com.qianniu.napi.admin.service.IOrderGoodsService;
import com.qianniu.napi.admin.service.IOrderService;
import com.qianniu.napi.common.util.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.*;

/**
 * <p>
 * 评论
 * </p>
 *
 * @author chenxuebin
 * @since 2018-07-06
 */
@RestController
public class CommentController {

    private static final Logger logger = LoggerFactory.getLogger(CommentController.class);

    @Autowired
    private ICommentService commentService;

    @Autowired
    private IOrderService orderService;
    @Autowired
    private IOrderGoodsService orderGoodsService;

    @Autowired
    private IGoodsfavService goodsfavService;

    @RequestMapping("/api/comment/list")
    @ResponseBody
    public Jinfo list(@RequestParam(value = "pageIndex", required = false) int pageIndex,
                      @RequestParam(value = "pageSize", required = false) int pageSize,
                      @RequestParam(value = "gid", required = false) long gid,
                      HttpServletRequest req) {
        Jinfo json = new Jinfo();
        Page page = new Page(pageIndex, pageSize);
        Comment myComment = null;
        Page<Comment> p = commentService.selectPage(page, new EntityWrapper<Comment>()
                .eq("gid", gid)
                .eq("state", 1).orderBy("id", false));
//        json.setData(page.getRecords());
        List<Comment> list = p.getRecords();
        for (Comment item : list) {
            if (!Strings.isNullOrEmpty(item.getPic())) {
                item.setPicList(Arrays.asList(item.getPic().split(";")));
            }
        }

        json.setInfo(list);
        return json;
    }

    @RequestMapping("/api/comment/add")
    @ResponseBody
    public Jinfo commentAdd(@Valid Comment comment, BindingResult bindingResult) {
        Jinfo json = new Jinfo();
        // 字段校验
        if (bindingResult.hasErrors()) {
            logger.debug("There are errors! {}", bindingResult);
            json.setMsg("There are errors! " + bindingResult);
            json.setCode(400);
            return json;
        }
        comment.setUid(ObjectUtil.getUid());
        comment.setMid(ObjectUtil.getMid());
        comment.setState(1);
        comment.setCreatetime(new Date());
        comment.insert();

        Order order = new Order();
        order.setId(comment.getOid());
        order.setIsComment(1);
        order.setStatus(StatusEnum.finish.getVal());
        orderService.updateById(order);

        OrderGoods og = new OrderGoods();
//        og.setOid(comment.getOid());
//        og.setOid(comment.getGid());
        og.setIsComment(1);
        og.setStatus(StatusEnum.finish.getVal());

        og.update(new EntityWrapper<OrderGoods>().eq("oid", comment.getOid()).eq("gid", comment.getGid()));
        return json;
    }

    @RequestMapping("/api/comment/modify")
    @ResponseBody
    public Jinfo commentUpdate(HttpServletRequest req, @Valid Comment comment) {
        Jinfo json = new Jinfo();
        comment.setModifytime(new Date());
        comment.updateById();
        return json;
    }

    @RequestMapping("/api/comment/detail")
    @ResponseBody
    public Jinfo getCommentDetailById(@RequestParam("token") String token,
                                      @RequestParam("id") long id, HttpServletRequest req
    ) {
        Jinfo json = new Jinfo();
        Comment comment = commentService.selectById(id);

        json.setInfo(comment);
        return json;
    }

    @RequestMapping("/api/proudct/commentInfo")
    @ResponseBody
    public Jinfo getProudctCommentInfo(@RequestParam("gid") long gid, HttpServletRequest req) {
        Jinfo json = new Jinfo();
        List<Comment> comment = commentService.selectList(new EntityWrapper<Comment>().eq("gid", gid));
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("comments", comment);
        map.put("count", comment == null ? 0 : comment.size());

        Goodsfav goodsfav = goodsfavService.selectOne(new EntityWrapper<Goodsfav>()
                .eq("gid", gid)
                .eq("uid", ObjectUtil.getUid())
                .orderBy("id", false));

        map.put("isCollection", goodsfav == null ? false : true);
        json.setInfo(map);
        return json;
    }

}
